package com.taobao.snowsnow.hibernate.temp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Utilities;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PushbuttonField;
import com.itextpdf.text.pdf.RadioCheckField;
import com.itextpdf.text.pdf.TextField;
 
public class AddJavaScriptToForm {
    /** The resulting PDF file. */
    public static final String ORIGINAL
        = "c:/form_without_js.pdf";
    /** The resulting PDF file. */
    public static final String RESULT
        = "c:/form_with_js.pdf";
    /** Path to the resources. */
    public static final String RESOURCE
        = "c:/extra.js";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException 
     * @throws    SQLException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
    	// step 1
    	float oneMM = (float) (72 / 25.4);
    	Document document = new Document(new Rectangle(231 * oneMM, 126 * oneMM));
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        BaseFont bf = BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        PdfContentByte directcontent = writer.getDirectContent();
        directcontent.beginText();
        directcontent.setFontAndSize(bf, 12);
        
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "安娜", 38 * oneMM, (126 - 36) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "柳州", 80 * oneMM, (126 - 36) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "淘宝旺旺ID: myleft1", 45 * oneMM, (126 - 45) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "http://snowsnow.taobao.com", 35 * oneMM, (126 - 53) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "雪雪原创 蝴蝶结专卖店 婚纱礼服蝴蝶结定制", 18 * oneMM, (126 - 62) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "0772-2818316", 47 * oneMM, (126 - 70) * oneMM - 10, 0);
        
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "黄颖淇", 135 * oneMM, (126 - 36) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "清远市", 176 * oneMM, (126 - 36) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "ID: 黄颖淇_007", 135 * oneMM, (126 - 45) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "广东省清远市佛冈县石角镇环城东路70", 125 * oneMM, (126 - 53) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "号 佛冈供电局 三楼营业部", 115 * oneMM, (126 - 62) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "13413545941", 137 * oneMM, (126 - 69) * oneMM - 10, 0);
        
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "蝴蝶结", 32 * oneMM, (126 - 97) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "12", 16 * oneMM, (126 - 110) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "9", 31 * oneMM, (126 - 110) * oneMM - 10, 0);
        directcontent.showTextAligned(Element.ALIGN_JUSTIFIED, "24", 40 * oneMM, (126 - 110) * oneMM - 10, 0);
        directcontent.endText();
        // step 5
        document.close();
    }
 
    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    public void manipulatePdf(String src, String dest)
        throws IOException, DocumentException {
    	// Create a reader and a stamper
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        // Get the writer from the stamper
        PdfWriter writer = stamper.getWriter();
        // Add JavaScript
        writer.addJavaScript("this.print({bUI: false,bSilent: true,bShrinkToFit: false});");
        // get the form fields
        AcroFields form = stamper.getAcroFields();
        AcroFields.Item fd = form.getFieldItem("married");
        // Get the PDF dictionary of the YES radio button and add an additional action
        PdfDictionary dictYes =
            (PdfDictionary) PdfReader.getPdfObject(fd.getWidgetRef(0));
        PdfDictionary yesAction = dictYes.getAsDict(PdfName.AA);
        if (yesAction == null) yesAction = new PdfDictionary();
        yesAction.put(new PdfName("Fo"),
                PdfAction.javaScript("setReadOnly(false);", stamper.getWriter()));
        dictYes.put(PdfName.AA, yesAction);
        // Get the PDF dictionary of the NO radio button and add an additional action
        PdfDictionary dictNo =
            (PdfDictionary) PdfReader.getPdfObject(fd.getWidgetRef(1));
        PdfDictionary noAction = dictNo.getAsDict(PdfName.AA);
        if (noAction == null) noAction = new PdfDictionary();
        noAction.put(new PdfName("Fo"),
                PdfAction.javaScript("setReadOnly(true);", stamper.getWriter()));
        dictNo.put(PdfName.AA, noAction);
        // Create a submit button and add it to the stamper
        PushbuttonField button = new PushbuttonField(writer,
                new Rectangle(40, 690, 200, 710), "submit");
        button.setText("validate and submit");
        button.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
        PdfFormField validateAndSubmit = button.getField();
        validateAndSubmit.setAction(PdfAction.javaScript("validate();", stamper.getWriter()));
        stamper.addAnnotation(validateAndSubmit, 1);
        // close the stamper
        stamper.close();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     * @throws SQLException
     */
    public static void main(String[] args)
        throws IOException, DocumentException {
        AddJavaScriptToForm example = new AddJavaScriptToForm();
        example.createPdf(ORIGINAL);
        //example.manipulatePdf(ORIGINAL, RESULT);
    }
}
 
